package ynu.sxp.demoapp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.sxp.demoapp.entity.DeliveryAddress;
import ynu.sxp.demoapp.service.DeliveryAddressService;
import ynu.sxp.demoapp.common.Result;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {

    @Autowired
    private DeliveryAddressService deliveryAddressService;

    // 创建新的送货地址
    @PostMapping("/saveDeliveryAddress")
    public Result<DeliveryAddress> saveDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) {
        DeliveryAddress savedAddress = deliveryAddressService.createDeliveryAddress(deliveryAddress);
        return Result.success(savedAddress);
    }

    // 根据ID获取送货地址
    @GetMapping("/getDeliveryAddressById")
    public Result<DeliveryAddress> getDeliveryAddressById(@RequestParam UUID daId) {
        DeliveryAddress address = deliveryAddressService.getDeliveryAddressById(daId);
        return Result.success(address);
    }

    // 获取用户的所有送货地址
    @GetMapping("/listDeliveryAddressByUserId")
    public Result<List<DeliveryAddress>> listDeliveryAddressByUserId(@RequestParam String userId) {
        List<DeliveryAddress> addresses = deliveryAddressService.getDeliveryAddressesByUserId(userId);
        return Result.success(addresses);
    }

    // 更新送货地址
    @PostMapping("/updateDeliveryAddress")
    public Result<Void> updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress) {
        deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        return Result.success(null);
    }

    // 删除送货地址
    @DeleteMapping("/removeDeliveryAddress")
    public Result<Void> removeDeliveryAddress(@RequestParam UUID daId) {
        deliveryAddressService.deleteDeliveryAddress(daId);
        return Result.success(null);
    }
}